package DAO;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import DBUntil.DbHelper;
import each_class.Album;
import each_class.RanKingClass;
import each_class.User;

public class Song_ListDAO {
	private static DbHelper db = new DbHelper();

	// 判断歌单是否存在
	public static boolean judgeExist(String uid, String listID) throws IOException {
		String sql = "select list_id from song_list where uid=? and list_id=?";
		Map<String, Object> map = db.findsingle(sql, uid, listID);
		if (null == map) {
			return true;
		}
		return false;
	}

	// 判断歌曲是否存在
	public static boolean judgeExist(String songid) throws IOException {
		String sql = "select list_id from song_list where song_id=? ";
		Map<String, Object> map = db.findsingle(sql, songid);
		if (null == map) {
			return true;
		}
		return false;
	}
	

	// 添加歌单
	public static int addSong_list(User us, RanKingClass ranKingClass) throws Exception {
		if (judgeExist(us.getUser_id(), ranKingClass.getId())) {
			String sql = "insert into song_list values(seq_list_lid.nextval,?,?,null,?,?)";
			return db.update(sql, us.getUser_id(), ranKingClass.getId(), ranKingClass.getRanKingClass_name(),
					ranKingClass.getRanKing_image_url());
		} else {
			return 0;
		}
	}

	// 添加专辑
	public static int addSong_list(User us, Album album) throws Exception {
		if (judgeExist(us.getUser_id(), album.getAlbm_id())) {
			String sql = "insert into song_list values(seq_list_lid.nextval,?,?,null,?,?)";
			return db.update(sql, us.getUser_id(), album.getAlbm_id(), album.getAlbm_name(), album.getAlbm_image_url());
		} else {
			return 0;
		}
	}
	
	//添加歌曲
	public static void addSong(User us,String listID , String songID) throws Exception {
		if(judgeExist(songID)) {
			String sql = "insert into song_list values(seq_list_lid.nextval,?,?,?,null,null)";
			db.update(sql, us.getUser_id(),listID,songID);
		}
	}
	

	// 查找某用户创建的歌单
	public static List<Map<String, Object>> findAll(String user_id) throws IOException {
		String sql = "select list_name from song_list where user_id=?";
		List<Map<String, Object>> lm = db.findMutip(sql, user_id);
		return lm;
	}

	// 查找某歌单
	public static Map<String, Object> find(String user_id, String list_id) throws IOException {
		String sql = "select list_id,list_name,list_imageurl from song_list where user_id=? and list_id=?";
		Map<String, Object> map = db.findsingle(sql, user_id, list_id);
		return map;
	}

	// 查找某歌单中的歌
	public static List<Map<String, Object>> findSongAll(String user_id, String list_id) throws IOException {
		String sql = "select song_id from song_list where user_id=? and list_id=?";
		List<Map<String, Object>> lm = db.findMutip(sql, user_id, list_id);
		return lm;
	}

	// 删除歌单
	public static int disSong_list(String user_id, String list_id) throws Exception {
		String sql = "delete from song_list where user_id=? and list_id=?";
		return db.update(sql, user_id, list_id);
	}

	// 删除歌
	public static int disSong(String user_id, String list_id, String songid) throws Exception {
		String sql = "delete from song_list where user_id=? and list_id=? and songid=?";
		return db.update(sql, user_id, list_id, songid);
	}
}
